/*
Copyright 2015 chanven

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package in.srain.cube.views.ptr.demo.ui.activity;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreRecyclerViewContainer;
import in.srain.cube.views.loadmore.RecyclerAdapterWithHF;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.demo.R;
import in.srain.cube.views.ptr.demo.ui.views.LoadMoreFooterView;

/**
 * RecyclerView with loadmore
 *
 * @author Chanven
 * @date 2015-9-21
 */
public class RecyclerViewActivity extends Activity {
    PtrClassicFrameLayout ptrClassicFrameLayout;
    RecyclerView mRecyclerView;
    private List<String> mData = new ArrayList<String>();
    private RecyclerAdapter adapter;
    private RecyclerAdapterWithHF mAdapter;
    private LoadMoreRecyclerViewContainer mLoadMore;
    Handler handler = new Handler();
    private  int mPage=0;

    int page = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview_layout);
        ptrClassicFrameLayout = (PtrClassicFrameLayout) this.findViewById(R.id.test_recycler_view_frame);
        mLoadMore= (LoadMoreRecyclerViewContainer) findViewById(R.id.load_more_recycler_view_container);
        mRecyclerView = (RecyclerView) this.findViewById(R.id.test_recycler_view);
        init();
    }

    private void init() {
        adapter = new RecyclerAdapter(this, mData);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        View headView =new View(this);
        View footView =new View(this);
        headView.setBackgroundColor(Color.RED);
        footView.setBackgroundColor(Color.GREEN);
        headView.setLayoutParams( new LinearLayout.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,300));
        footView.setLayoutParams( new LinearLayout.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,300));
        mAdapter.addHeader(headView);
        mAdapter.addFooter(footView);
        ptrClassicFrameLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh(true);
            }
        }, 150);

        ptrClassicFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {

                // here check list view, not content.
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                mPage=0;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mLoadMore.loadMoreFinish(false,true);
                        page = 0;
                        mData.clear();
                        for (int i = 0; i < 20; i++) {
                            mData.add(new String("  RecyclerView item  -" + i));
                        }
                        mAdapter.notifyDataSetChanged();
                        ptrClassicFrameLayout.refreshComplete();
                      // ptrClassicFrameLayout.setLoadMoreEnable(true);
                    }
                }, 1500);
            }
        });

        LoadMoreFooterView moreFooterView = new LoadMoreFooterView(this);
        mLoadMore.loadMoreFinish(false,true);
        mLoadMore.setLoadMoreView(moreFooterView);
        mLoadMore.setLoadMoreUIHandler(moreFooterView);
        mLoadMore.initFooterView();
        mLoadMore.setLoadMoreHandler(new LoadMoreHandler() {
            @Override
            public void onLoadMore(final LoadMoreContainer loadMoreContainer) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPage++;
                        for (int i = 0; i < 5; i++) {
                            mData.add(new String("  RecyclerView item  -add" + i));
                        }
                        if (mPage == 3) {
                            loadMoreContainer.loadMoreFinish(false,false);

                        }else {
                            loadMoreContainer.loadMoreFinish(false,true);
                        }
                        mAdapter.notifyDataSetChangedHF();

                    }
                },1000);


            }
        });



//        ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void loadMore() {
//                handler.postDelayed(new Runnable() {
//
//                    @Override
//                    public void run() {
//                        mData.add(new String("  RecyclerView item  - add " + page));
//                        mAdapter.notifyDataSetChanged();
//                        ptrClassicFrameLayout.loadMoreComplete(true);
//                        page++;
//                        Toast.makeText(RecyclerViewActivity.this, "load more complete", Toast.LENGTH_SHORT).show();
//                    }
//                }, 1000);
//            }
//        });
    }

    public class RecyclerAdapter extends Adapter<ViewHolder> {
        private List<String> datas;
        private LayoutInflater inflater;
        public RecyclerAdapter(Context context, List<String> data) {
            super();
            inflater = LayoutInflater.from(context);
            datas = data;
        }
        @Override
        public int getItemCount() {
            return datas.size();
        }
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            ChildViewHolder holder = (ChildViewHolder) viewHolder;
            holder.itemTv.setText(datas.get(position));
        }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewHolder, int position) {
            View view = inflater.inflate(R.layout.item_recyclerview_layout, null);
            return new ChildViewHolder(view);
        }
    }
    public class ChildViewHolder extends ViewHolder {
        public TextView itemTv;
        public ChildViewHolder(View view) {
            super(view);
            itemTv = (TextView) view;
        }

    }
}
